package com.mihu.stmall.manager.mapper;




import com.mihu.stmall.manager.entiy.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Mapper层接口
 * 基于注解的开发
 */
@Mapper  //MyBatis的注解 当前的接口是MyBatis的接口 相当于Application上添加@MapperScan注解
public interface GoodsMapper {

    /**
     * 插入数据表的操作
     * 1.返回值类型 int
     * 2.方法名 insert
     * 3.参数 Goods
     * 4.配置 使用@Insert的注解配置SQL语句
     */
    @Insert({"insert into goods",
            "(name,dscp,price,mark_price,color,version,count,content,recom,category_id)",
            "value(#{name},#{dscp},#{price},#{markPrice},#{color},#{version},#{count},#{content},#{recom},#{categoryId})",
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(Goods goods);

    /**
     * 根据ID删除
     */
    @Delete("delete from goods where id=#{id}")
    int delete(Integer id);

    /**
     * 根据ID更新 使用动态SQL,需要为SQL语句添加<script></script>
     */
    @Update({"<script>",
            "update goods",
            "<set>",
            "<if test='name != null and name.length>0'>name=#{name},</if>",
            "<if test='dscp != null and dscp.length>0'>dscp=#{dscp},</if>",
            "<if test='price != null'>price=#{price},</if>",
            "<if test='markPrice != null'>mark_price=#{markPrice},</if>",
            "<if test='color != null and color.length>0'>color=#{color},</if>",
            "<if test='version != null and version.length>0'>version=#{version},</if>",
            "<if test='count != null'>count=#{count},</if>",
            "<if test='content != null and content.length>0'>content=#{content},</if>",
            "<if test='recom != null'>recom=#{recom},</if>",
            "<if test='categoryId != null'>category_id=#{categoryId},</if>",
            "<if test='status != null'>status=#{status},</if>",

            "</set>",
            "where id=#{id}",
            "</script>",
    })
    int update(Goods goods);

    /**
     * 根据ID查询
     */
    @Select("select * from goods where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "pics",
            many = @Many(select = "com.mihu.stmall.manager.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    Goods selectById(Integer id);

    /**
     * 通用的多条件的组合查询
     */
    @Select({"<script>",
            "select * from goods",
            "<where>",
            "<if test='name != null and name.length>0'>name like '%${name}%'</if>",
            "<if test='categoryId != null and categoryId!=0'> and category_id = #{categoryId} </if>",
            "<if test='categoryId != null and categoryId==0'> and category_id is null</if>",
            "<if test='recom != null'> and recom = #{recom} </if>",
            "<if test='status != null'> and status = #{status} </if>",
            "</where>",
            "</script>"
    })
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "category_id",property = "category",
            one=@One(select = "com.mihu.stmall.manager.mapper.CategoryMapper.selectById")),
            @Result(column = "id",property = "pics",
                    many = @Many(select = "com.mihu.stmall.manager.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    List<Goods> select(Goods goods);
}
